package com.gy.studyonlineapp.fragment;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.gy.studyonlineapp.R;
import com.gy.studyonlineapp.common.MyApplication;
import com.gy.studyonlineapp.databinding.FragmentJavaQuestionBinding;
import java.util.Map;

public class JavaQuestionFragment extends Fragment {

    private String title;
    private String selectStr;
    private String answer;
    private String objectId;
    private FragmentJavaQuestionBinding binding;

    public static JavaQuestionFragment newInstance(String title, String selectStr, String answer, String objectId) {
        JavaQuestionFragment fragment = new JavaQuestionFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("selectStr", selectStr);
        args.putString("answer", answer);
        args.putString("objectId", objectId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        title = getArguments() != null ? getArguments().getString("title") : " ";
        selectStr = getArguments() != null ? getArguments().getString("selectStr") : " ";
        answer = getArguments() != null ? getArguments().getString("answer") : " ";
        objectId = getArguments() != null ? getArguments().getString("objectId") : " ";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_java_question, container, false);
        binding.javaContent.setText(title);
        // 为每个题目设置ID
        binding.objectId.setText(objectId);
        String[] split = selectStr.split(";");
        binding.rbOptionA.setText(split[0].charAt(0) + " " + split[0].substring(1));
        binding.rbOptionB.setText(split[1].charAt(0) + " " + split[1].substring(1));
        binding.rbOptionC.setText(split[2].charAt(0) + " " + split[2].substring(1));
        binding.rbOptionD.setText(split[3].charAt(0) + " " + split[3].substring(1));
        binding.rgBase.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton rb = binding.getRoot().findViewById(radioGroup.getCheckedRadioButtonId());
                Map<String, String> userAns = ((MyApplication) binding.getRoot().getContext().getApplicationContext()).getUserAns();
                Map<String, String> standardAns = ((MyApplication) binding.getRoot().getContext().getApplicationContext()).getStandardAns();
                // 添加用户的答案
                userAns.put(objectId, rb.getText().toString().split(" ")[0]);
                // 添加标准答案
                standardAns.put(objectId, answer);
            }
        });
        return binding.getRoot();
    }
}